package com.nexgo.payment.trans.scansale;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;

import com.nexgo.payment.R;
import com.nexgo.payment.action.inputpaycode.InputPayCodeFragment;
import com.nexgo.payment.action.scan.BccScanFragment;
import com.nexgo.payment.action.scan.BccScanOrderFragment;
import com.nexgo.payment.action.scan.ScanFragment;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.fragment.TextDialogFragment;
import com.nexgo.payment.scan.ScanCameraActivity;
import com.nexgo.payment.scan.ScanNFragment;
import com.nexgo.payment.trans.AbsTransActivity;
import com.nexgo.payment.util.ActivityUtils;
import com.nexgo.payment.util.ToastUtils;

/**
 * 扫码支付
 * Created by wanghongyang on 2017/9/1.
 */
public class ScanSaleActivity extends AbsTransActivity<ScanSaleContract.Presenter> implements ScanSaleContract.View{

    private boolean mFirstTime = true;
    private static final String SCANFLAG = "scan_flag";
    private static final String CHANNEL = "channel_flag";
    private Boolean scanFlag = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        String channel = new String();
        if(bundle != null){
            Log.d("Scan", "onCreate: 下单收款");
            scanFlag = bundle.getBoolean(SCANFLAG,false);
            channel = bundle.getString(CHANNEL);
        }
        Log.d("Scan", "onCreate:");
        new ScanSalePresenter(this, Repository.getInstance(),scanFlag,channel);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
        //第一次调用不起作用，扫码返回后再次调用才起作用
        mPresenter.showOnScanNextAndRestart();
    }

    @Override
    public void showScanFragment(TransData transData) {
        BccScanFragment scanFragment = BccScanFragment.newInstance(transData);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        }
    }

    @Override
    public void showInputPayCode() {
        InputPayCodeFragment payCodeFragment = InputPayCodeFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), payCodeFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), payCodeFragment, R.id.contentFrame);
        }
    }

    @Override
    public void showNeedScanQuery(int times) {
        TextDialogFragment dialogFragment = TextDialogFragment.newInstance();
        dialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_WRONG);
        dialogFragment.setTitleText(getString(R.string.scan_query_start));
        dialogFragment.setContentText(getString(R.string.scan_need_query), Gravity.CENTER);
        dialogFragment.setPositionText(getString(R.string.confirm));
        dialogFragment.setNegativeText(getString(R.string.cancel));
        dialogFragment.setDownTimer(SysConstant.TIME_OUT_10, true);
        dialogFragment.setDialogListener(new TextDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {        //发送撤销
                mFirstTime = true;
                dialogFragment.dismiss();
                if(scanFlag) {
                    mPresenter.ShowCancelScan();
                }else{
                    mPresenter.ShowVoidScan();
                }
            }
            @Override
            public void onConfirm() {       //确认发送查询
//                quitTrans();
                dialogFragment.dismiss();
                mPresenter.ShowQueryScan(mFirstTime);
                mFirstTime = false;
            }
        });
        dialogFragment.show(getSupportFragmentManager(), null);
    }

    @Override
    public void showScanOrderFragment(TransData transData) {
        BccScanOrderFragment scanOrderFragment = BccScanOrderFragment.newInstance(transData);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), scanOrderFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), scanOrderFragment, R.id.contentFrame);
        }
    }
/*
    @Override
    public void showScanFragment() {
        ScanFragment scanFragment = ScanFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        }
    }*/
    @Override
    public void showScanFragment() {
            /*ScanFragment scanFragment = ScanFragment.newInstance();
            Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
            if (fragment == null) {
                ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
            } else {
                ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
            }*/
        startActivityForResult(ScanCameraActivity.newInstance(this), ScanCameraActivity.SCAN_REQUEST_CODE);
    }

    private void addScanNFragment(){
        ScanNFragment scanFragment = ScanNFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ScanCameraActivity.SCAN_REQUEST_CODE){
            if(resultCode == ScanCameraActivity.SCAN_RESULT_SUCCESS_CODE){
                if(data == null || TextUtils.isEmpty(data.getStringExtra(ScanCameraActivity.RESULT_MSG))){
                    ToastUtils.showToast("扫码失败");
                    finish();
                }
                addScanNFragment();
                mPresenter.inputScanNext(data.getStringExtra(ScanCameraActivity.RESULT_MSG));
                //第一次调用不起作用，扫码返回后再次调用才起作用
                mPresenter.showOnScanNextAndRestart();
                return;
            }
            if(data != null){
                String eMsg =  data.getStringExtra(ScanCameraActivity.RESULT_MSG);
                ToastUtils.showToast(eMsg);
                finish();
            }

        }
    }
    @Override
    public void showTradeFail(String resMessage) {
        TextDialogFragment dialogFragment = TextDialogFragment.newInstance();
        dialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_WRONG);
        dialogFragment.setContentText(resMessage, Gravity.CENTER);
        dialogFragment.setPositionText(getString(R.string.confirm));
        dialogFragment.setDownTimer(SysConstant.TIME_OUT_10, true);
        dialogFragment.setDialogListener(new TextDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {
                quitTrans();
            }

            @Override
            public void onConfirm() {
                quitTrans();
            }
        });
        dialogFragment.show(getSupportFragmentManager(), null);
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, ScanSaleActivity.class);
    }

    //lr flag 0-被扫 1-主扫
    public static Intent newInstance(Context context, Boolean scanFlag, String channel){
        Intent intent = new Intent(context, ScanSaleActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(SCANFLAG,scanFlag);
        bundle.putString(CHANNEL,channel);
        intent.putExtras(bundle);
        return intent;
    }

}
